<!--
@license
Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="tf-smoothing-input.html">
<link rel="import" href="../tf-backend/tf-backend.html">
<link rel="import" href="../tf-color-scale/tf-color-scale.html">
<link rel="import" href="../tf-dashboard-common/tf-dashboard.html">
<link rel="import" href="../tf-dashboard-common/tf-option-selector.html">
<link rel="import" href="../tf-dashboard-common/tf-panes-helper.html">
<link rel="import" href="../tf-dashboard-common/tf-sidebar-helper.html">
<link rel="import" href="../tf-imports/lodash.html">
<link rel="import" href="../vz-line-chart/vz-line-chart.html">
<link rel="import" href="../iron-collapse/iron-collapse.html">
<link rel="import" href="../paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../paper-menu/paper-menu.html">
<link rel="import" href="../paper-item/paper-item.html">

<!--
tf-event-dashboard is a complete frontend that loads runs from a backend,
and creates chart panes that display data for those runs.

It provides a categorizer, run selector, and x type selector, by which the user
can customize how data is organized and displayed.

Each chart has a button that can toggle whether it is "expanded"; expanded
charts are larger.

Organizationally, the #plumbing div contains components that have no concrete
manifestation and just effect data bindings or data loading. The .sidebar div
contains shared controls provided by tf-sidebar-helper. The .center div
contains vz-line-charts embedded inside tf-panes-helper's.
-->
<dom-module id="tf-event-dashboard">
  <template>
    <div id="plumbing">
      <tf-color-scale
        id="colorScale"
        runs="[[runs]]"
        out-color-scale="{{_colorScale}}"
      ></tf-color-scale>
    </div>

    <tf-dashboard-layout>
      <div class="sidebar">
        <tf-sidebar-helper
          categories="{{_categories}}"
          color-scale="[[_colorScale]]"
          run2tag="[[run2tag]]"
          runs="[[runs]]"
          selected-runs="{{_selectedRuns}}"
          >
          <div class="extend-first-section">
            <paper-checkbox
              id="download-option"
              checked="{{_showDownloadLinks}}"
              >Data download links</paper-checkbox>
            <div id="tooltip-sorting">
              <div id="tooltip-sorting-label">Tooltip sorting method:</div>
              <paper-dropdown-menu
                no-label-float
                selected-item-label="{{_tooltipSortingMethod}}"
                >
                <paper-menu class="dropdown-content" selected="0">
                  <paper-item>default</paper-item>
                  <paper-item>descending</paper-item>
                  <paper-item>ascending</paper-item>
                </paper-menu>
              </paper-dropdown-menu>
            </div>
          </div>
          <div class="sidebar-section">
            <tf-smoothing-input
              weight="{{_smoothingWeight}}"
              step="0.001"
              min="0"
              max="1"
              ></tf-smoothing-input>
          </div>
          <div class="sidebar-section">
            <tf-option-selector
              id="xTypeSelector"
              name="Horizontal Axis"
              selected-id="{{_xType}}"
              >
              <paper-button id="step">step</paper-button>
              <paper-button id="relative">relative</paper-button>
              <paper-button id="wall_time">wall</paper-button>
            </tf-option-selector>
          </div>
        </tf-sidebar-helper>
      </div>
      <div class="center">
        <tf-panes-helper
          categories="[[_categories]]"
          color-scale="[[_colorScale]]"
          data-type="[[dataType]]"
          data-provider="[[dataProvider]]"
          run2tag="[[run2tag]]"
          selected-runs="[[_selectedRuns]]"
          show-download-links="[[_showDownloadLinks]]"
          download-link-url-function="[[scalarUrl]]"
          >
          <template>
            <vz-line-chart
              x-type="[[_xType]]"
              color-scale="[[_colorScale]]"
              smoothing-enabled="[[_smoothingEnabled]]"
              smoothing-weight="[[_smoothingWeight]]"
              tooltip-sorting-method="[[_tooltipSortingMethod]]"
              ></vz-line-chart>
            <paper-icon-button
              class="log-button"
              icon="line-weight"
              on-tap="toggleLogScale"
              title="Toggle y-axis log scale"
              ></paper-icon-button>
          </template>
        </tf-panes-helper>
      </div>
    </tf-dashboard-layout>

    <style include="dashboard-style"></style>
    <style>
      .log-button {
        position: absolute;
        left: 35px;
        bottom: -35px;
        color: #2196F3;
        background: #fff;
        width: 32px;
        height: 32px;
        padding: 4px;
        border-radius: 100%;
      }

      .log-button-selected {
        background: var(--tb-ui-light-accent);
      }

      #tooltip-sorting {
        display: flex;
        font-size: 14px;
        margin-top: 5px;
      }

      #tooltip-sorting-label {
        margin-top: 13px;
        margin-left: 28px;
      }

      #tooltip-sorting paper-dropdown-menu {
        margin-left: 10px;
        --paper-input-container-focus-color: var(--tb-orange-strong);
        width: 105px;
      }
    </style>

  </template>

  <script>
    Polymer({
      is: "tf-event-dashboard",
      behaviors: [
        TF.Dashboard.ReloadBehavior("tf-chart-scaffold"),
        TF.Backend.Behavior,
      ],
      properties: {
        dataType: {
          type: String,
          value: "scalar"
        },
        router: Object,
        scalarUrl: {
          type: Function,
          computed: "_getScalarUrl(router)"
        },
        _showDownloadLinks: {
          type: Boolean,
          notify: true,
          value: TF.URIStorage.getBooleanInitializer('_showDownloadLinks',
              false),
          observer: '_showDownloadLinksObserver'
        },
        _smoothingWeight: {
          type: Number,
          notify: true,
          value: TF.URIStorage.getNumberInitializer('_smoothingWeight', 0.6),
          observer: '_smoothingWeightObserver'
        },
        _smoothingEnabled: {
          type: Boolean,
          computed: '_computeSmoothingEnabled(_smoothingWeight)'
        },
        _xType: {
          type: String,
          value: "step"
        }
      },
      attached: function() {
        this.async(function() {
          this.fire("rendered");
        });
      },
      _getScalarUrl: function() {
        return this.router.scalars;
      },
      _showDownloadLinksObserver: TF.URIStorage.getBooleanObserver(
          '_showDownloadLinks', false),
      _smoothingWeightObserver: TF.URIStorage.getNumberObserver(
          '_smoothingWeight', 0.6),
      _computeSmoothingEnabled: function(_smoothingWeight) {
        return _smoothingWeight > 0;
      },
      toggleLogScale: function(e) {
        var currentTarget = Polymer.dom(e.currentTarget);
        var button = currentTarget.parentNode.querySelector('.log-button');
        var chart = currentTarget.parentNode.querySelector('vz-line-chart');

        button.classList.toggle("log-button-selected");
        chart.yScaleType = chart.yScaleType === 'log' ? 'linear' : 'log';
        chart.redraw();
      },
    });
  </script>
</dom-module>
